package android.pot.com.smartcar.ui.find;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.pot.com.smartcar.R;
import android.pot.com.smartcar.base.swipe.SwipeBackActivity;
import android.pot.com.smartcar.tool.LoadingTool;
import android.pot.com.smartcar.widget.flowTag.FlowsLayout;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;

import java.io.File;

import static android.pot.com.smartcar.util.Utils.getScreenWidth;

/**
 * Created by zhaoshilu on 2017/11/27.
 */
public class PhotoActivity extends SwipeBackActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_photos);
        initView();
        addListener();
        initData();
    }

    @Override
    protected void initView() {
        Bundle bundle = getIntent().getExtras();
        String img = bundle.getString("img");
        final ImageView imageView = (ImageView) findViewById(R.id.test_image);

        LoadingTool.StartLoading(this);
        Glide.with(this).load(img)
                .downloadOnly(new SimpleTarget<File>() {
                    @Override
                    public void onResourceReady(File resource, GlideAnimation<? super File> glideAnimation) {
                        LoadingTool.EndLoading(PhotoActivity.this);

                        // 将保存的图片地址给SubsamplingScaleImageView,这里注意设置ImageViewState设置初始显示比例
                        BitmapFactory.Options options = new BitmapFactory.Options();
                        options.inTempStorage = new byte[1024*1024*10];
                        options.inSampleSize = 2;
                        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
                        Bitmap bitmap= BitmapFactory.decodeFile(resource.getAbsolutePath(),options);
                        // 显示处理好的Bitmap图片
        int screenWidth = getScreenWidth(PhotoActivity.this);
        ViewGroup.LayoutParams lp = imageView.getLayoutParams();
        lp.width = screenWidth;
        lp.height = FlowsLayout.LayoutParams.WRAP_CONTENT;
        imageView.setLayoutParams(lp);

                        imageView.setImageBitmap(bitmap);

                    }
                });
//        int screenWidth = getScreenWidth(this);
//        ViewGroup.LayoutParams lp = imageView.getLayoutParams();
//        lp.width = screenWidth;
//        lp.height = FlowsLayout.LayoutParams.WRAP_CONTENT;
//        imageView.setLayoutParams(lp);
//
//        imageView.setMaxWidth(screenWidth);
//        imageView.setMaxHeight(screenWidth * 10); //这里其实可以根据需求而定，我这里测试为最大宽度的5倍
//        ImageLoadUtils.INSTANCE.loadImageView(imageView,img);
    }

    @Override
    protected void addListener() {
        findViewById(R.id.img_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                scrollToFinishActivity();
            }
        });


    }



    @Override
    protected void initData() {

    }


}